home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Documentation / d e v e l o p / 3D Game Controls / Source / camera.c < prev    next >
Encoding:
Text File  |  1997-08-14  |  10.7 KB  |  320 lines  |  [TEXT/MPCC]

  1. //--------------------------------------------------------------------------------------------
  2. //  Camera Functions
  3. //
  4. //      by Philip McBride
  5. //
  6. //--------------------------------------------------------------------------------------------
  7.  
  8.  
  9. #include <FixMath.h>
  10. #include "GameControls.h"
  11. #include "extern.h"
  12. #include "camera.h"
  13.  
  14. //--------------------------------------------------------------------------------------------
  15. //  Create a Camera (with default settings) -- used if no camera included in file
  16. //
  17. TQ3CameraObject MyNewCamera(CWindowPtr theWindow)
  18. {
  19.     TQ3ViewAngleAspectCameraData    perspectiveData;
  20.     TQ3CameraObject                    camera;
  21.     float                             fieldOfView = .50;
  22.     TQ3Status                        returnVal = kQ3Failure ;
  23.  
  24.     // Assign default placement and range
  25.     perspectiveData.cameraData.placement.cameraLocation     = kMyDefaultFrom;
  26.     perspectiveData.cameraData.placement.pointOfInterest     = kMyDefaultTo;
  27.     perspectiveData.cameraData.placement.upVector             = kMyDefaultUp;
  28.     perspectiveData.cameraData.range.hither    = kMyDefaultHither;
  29.     perspectiveData.cameraData.range.yon     = kMyDefaultYon;
  30.  
  31.     // Assign standard viewport
  32.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  33.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  34.     perspectiveData.cameraData.viewPort.width = 2.0;
  35.     perspectiveData.cameraData.viewPort.height = 2.0;
  36.     
  37.     perspectiveData.fov                = fieldOfView;
  38.     perspectiveData.aspectRatioXToY    =
  39.         (float) (theWindow->portRect.right - theWindow->portRect.left) / 
  40.         (float) (theWindow->portRect.bottom - theWindow->portRect.top);
  41.         
  42.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  43.     
  44.     return camera ;
  45. }
  46.  
  47. //--------------------------------------------------------------------------------------------
  48. //  Get a Model's Bounding Box
  49. //
  50. void MyGetBoundingBox(DocumentPtr theDocument, TQ3GroupObject mainGroup, TQ3BoundingBox *viewBBox)
  51. {
  52.     TQ3Status                    status;
  53.     TQ3ViewObject                 viewObject = theDocument->theView;
  54.     
  55.     Q3View_StartBoundingBox(viewObject,kQ3ComputeBoundsApproximate);
  56.     do {
  57.         status = Q3DisplayGroup_Submit(mainGroup, viewObject);
  58.     } while (Q3View_EndBoundingBox(viewObject, viewBBox) == kQ3ViewStatusRetraverse);                                        
  59. }
  60.  
  61. //--------------------------------------------------------------------------------------------
  62. //  Get the Camera Data (initialize for new file and camera)
  63. //
  64. // Get the camera information from the current view and initialize the
  65. // camera related fields of the document record
  66. void MyGetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
  67. {
  68.     TQ3CameraPlacement    cameraPlacement;
  69.  
  70.     // Get the camera data.
  71.     Q3Camera_GetPlacement(theCamera, &cameraPlacement);
  72.  
  73.     // Set the document's camera data.
  74.     theDocument->cameraLocation = cameraPlacement. cameraLocation;
  75.     theDocument->pointOfInterest = cameraPlacement. pointOfInterest;
  76.     theDocument->yVector = cameraPlacement. upVector;
  77.  
  78.     // Calculate the x and z vectors and assign them to the document.
  79.     Q3Point3D_Subtract(&theDocument->pointOfInterest,
  80.         &theDocument->cameraLocation, &theDocument->zVector);
  81.     Q3Vector3D_Cross(&theDocument->zVector, &theDocument->yVector,
  82.         &theDocument->xVector);
  83. }
  84.  
  85. //--------------------------------------------------------------------------------------------
  86. //  Set the Camera'a Data (when moved)
  87. //
  88. // Set the camera of the current view to the values in the document record
  89. void MySetCameraData(DocumentPtr theDocument, TQ3CameraObject theCamera)
  90. {
  91.     TQ3CameraPlacement    cameraPlacement;
  92.  
  93.     // Set the camera placement data.
  94.     cameraPlacement.cameraLocation = theDocument->cameraLocation;
  95.     cameraPlacement.pointOfInterest = theDocument->pointOfInterest;
  96.     cameraPlacement.upVector = theDocument->yVector;
  97.  
  98.     // Set the camera data to the camera.
  99.     Q3Camera_SetPlacement(theCamera, &cameraPlacement);
  100. }
  101.  
  102. //--------------------------------------------------------------------------------------------
  103. //  Move Camera X
  104. //
  105. // Move the camera along the X axis
  106. void MyMoveCameraX(DocumentPtr theDocument, float dX)
  107. {
  108.     TQ3ViewObject        theView;
  109.     TQ3CameraObject        theCamera;
  110.     TQ3Vector3D            scaledVector;
  111.     TQ3Point3D            newPoint;
  112.  
  113.     // Get the view and the camera objects.
  114.     theView = theDocument->theView;
  115.     Q3View_GetCamera(theView, &theCamera);
  116.  
  117.     // Scale the X vector to make it dX longer.
  118.     Q3Vector3D_Scale(&theDocument->xVector,dX/Q3Vector3D_Length(&theDocument->xVector),
  119.                         &scaledVector);
  120.  
  121.     // Move the camera position and direction by the new vector.
  122.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  123.     theDocument->cameraLocation = newPoint;
  124.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  125.     theDocument->pointOfInterest = newPoint;
  126.  
  127.     // Set the updated camera data to the camera.
  128.     MySetCameraData(theDocument, theCamera);
  129.  
  130.     // Update the view with the changed camera and dispose of the camera.
  131.     Q3View_SetCamera(theView, theCamera);
  132.     Q3Object_Dispose(theCamera);
  133. }
  134.  
  135. //--------------------------------------------------------------------------------------------
  136. //  Move Camera Y
  137. //
  138. // Move the camera along the Y axis
  139. void MyMoveCameraY(DocumentPtr theDocument, float dY)
  140. {
  141.     TQ3ViewObject        theView;
  142.     TQ3CameraObject        theCamera;
  143.     TQ3Vector3D            scaledVector;
  144.     TQ3Point3D            newPoint;
  145.  
  146.     // Get the view and the camera objects.
  147.     theView = theDocument->theView;
  148.     Q3View_GetCamera(theView, &theCamera);
  149.  
  150.     // Scale the Y vector to make it dY longer.
  151.     Q3Vector3D_Scale(&theDocument->yVector,dY/Q3Vector3D_Length(&theDocument->yVector),
  152.                         &scaledVector);
  153.  
  154.     // Move the camera position and direction by the new vector.
  155.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  156.     theDocument->cameraLocation = newPoint;
  157.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  158.     theDocument->pointOfInterest = newPoint;
  159.  
  160.     // Set the updated camera data to the camera.
  161.     MySetCameraData(theDocument, theCamera);
  162.  
  163.     // Update the view with the changed camera and dispose of the camera.
  164.     Q3View_SetCamera(theView, theCamera);
  165.     Q3Object_Dispose(theCamera);
  166. }
  167.  
  168. //--------------------------------------------------------------------------------------------
  169. //  Move Camera Z
  170. //
  171. // Move the camera along the Z axis
  172. void MyMoveCameraZ(DocumentPtr theDocument, float dZ)
  173. {
  174.     TQ3ViewObject        theView;
  175.     TQ3CameraObject        theCamera;
  176.     TQ3Vector3D            scaledVector;
  177.     TQ3Point3D            newPoint;
  178.  
  179.     // Get the view and the camera objects.
  180.     theView = theDocument->theView;
  181.     Q3View_GetCamera(theView, &theCamera);
  182.  
  183.     // Scale the Z vector to make it dZ longer.
  184.     Q3Vector3D_Scale(&theDocument->zVector,dZ/Q3Vector3D_Length(&theDocument->zVector),
  185.                         &scaledVector);
  186.  
  187.     // Move the camera position and direction by the new vector.
  188.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,&scaledVector,&newPoint);
  189.     theDocument->cameraLocation = newPoint;
  190.     Q3Point3D_Vector3D_Add(&theDocument->pointOfInterest,&scaledVector,&newPoint);
  191.     theDocument->pointOfInterest = newPoint;
  192.     
  193.     // Set the updated camera data to the camera.
  194.     MySetCameraData(theDocument, theCamera);
  195.  
  196.     // Update the view with the changed camera and dispose of the camera.
  197.     Q3View_SetCamera(theView, theCamera);
  198.     Q3Object_Dispose(theCamera);
  199. }
  200.  
  201. //--------------------------------------------------------------------------------------------
  202. //  Rotate Camera X
  203. //
  204. // Rotate the camera about the X axis
  205. void MyRotateCameraX(DocumentPtr theDocument, float dX)
  206. {
  207.     TQ3ViewObject        theView;
  208.     TQ3CameraObject        theCamera;
  209.     TQ3Vector3D            rotatedVector;
  210.     TQ3Matrix4x4        rotationMatrix;
  211.  
  212.     // Get the view and the camera objects.
  213.     theView = theDocument->theView;
  214.     Q3View_GetCamera(theView, &theCamera);
  215.  
  216.     // Create the rotation matrix for rotating about the x axis.
  217.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  218.         &theDocument->cameraLocation, &theDocument->xVector, dX);
  219.  
  220.     // Rotate the y vector (up vector) about the x axis.
  221.     Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
  222.         &rotatedVector);
  223.     theDocument->yVector = rotatedVector;
  224.  
  225.     // Rotate the z vector about the x axis.
  226.     Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
  227.         &rotatedVector);
  228.     theDocument->zVector = rotatedVector;
  229.  
  230.     // Update the point of interest from the new z vector.
  231.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
  232.         &theDocument->zVector, &theDocument->pointOfInterest);
  233.  
  234.     // Set the updated camera data to the camera.
  235.     MySetCameraData(theDocument, theCamera);
  236.  
  237.     // Update the view with the changed camera and dispose of the camera.
  238.     Q3View_SetCamera(theView, theCamera);
  239.     Q3Object_Dispose(theCamera);
  240. }
  241.  
  242. //--------------------------------------------------------------------------------------------
  243. //  Rotate Camera Y
  244. //
  245. // Rotate the camera about the Y axis
  246. void MyRotateCameraY(DocumentPtr theDocument, float dY)
  247. {
  248.     TQ3ViewObject        theView;
  249.     TQ3CameraObject        theCamera;
  250.     TQ3Vector3D            rotatedVector;
  251.     TQ3Matrix4x4        rotationMatrix;
  252.  
  253.     // Get the view and the camera objects.
  254.     theView = theDocument->theView;
  255.     Q3View_GetCamera(theView, &theCamera);
  256.  
  257.     // Create the rotation matrix for rotating about the y axis.
  258.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  259.         &theDocument->cameraLocation, &theDocument->yVector, dY);
  260.  
  261.     // Rotate the z vector about the y axis.
  262.     Q3Vector3D_Transform(&theDocument->zVector, &rotationMatrix,
  263.         &rotatedVector);
  264.     theDocument->zVector = rotatedVector;
  265.  
  266.     // Rotate the x vector about the y axis.
  267.     Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
  268.         &rotatedVector);
  269.     theDocument->xVector = rotatedVector;
  270.  
  271.     // Update the point of interest from the new z vector.
  272.     Q3Point3D_Vector3D_Add(&theDocument->cameraLocation,
  273.         &theDocument->zVector, &theDocument->pointOfInterest);
  274.  
  275.     // Set the updated camera data to the camera.
  276.     MySetCameraData(theDocument, theCamera);
  277.  
  278.     // Update the view with the changed camera and dispose of the camera.
  279.     Q3View_SetCamera(theView, theCamera);
  280.     Q3Object_Dispose(theCamera);
  281. }
  282.  
  283. //--------------------------------------------------------------------------------------------
  284. //  Rotate Camera Z
  285. //
  286. // Rotate the camera about the Z axis
  287. void MyRotateCameraZ(DocumentPtr theDocument, float dZ)
  288. {
  289.     TQ3ViewObject        theView;
  290.     TQ3CameraObject        theCamera;
  291.     TQ3Vector3D            rotatedVector;
  292.     TQ3Matrix4x4        rotationMatrix;
  293.  
  294.     // Get the view and the camera objects.
  295.     theView = theDocument->theView;
  296.     Q3View_GetCamera(theView, &theCamera);
  297.  
  298.     // Create the rotation matrix for rotating about the z axis.
  299.     Q3Matrix4x4_SetRotateAboutAxis(&rotationMatrix,
  300.         &theDocument->cameraLocation, &theDocument->zVector, dZ);
  301.  
  302.     // Rotate the y vector (up vector) about the z axis.
  303.     Q3Vector3D_Transform(&theDocument->yVector, &rotationMatrix,
  304.         &rotatedVector);
  305.     theDocument->yVector = rotatedVector;
  306.  
  307.     // Rotate the x vector about the z axis.
  308.     Q3Vector3D_Transform(&theDocument->xVector, &rotationMatrix,
  309.         &rotatedVector);
  310.     theDocument->xVector = rotatedVector;
  311.  
  312.     // Set the updated camera data to the camera.
  313.     MySetCameraData(theDocument, theCamera);
  314.  
  315.     // Update the view with the changed camera and dispose of the camera.
  316.     Q3View_SetCamera(theView, theCamera);
  317.     Q3Object_Dispose(theCamera);
  318. }
  319.  
  320.